package com.ustb.mainweb;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class AXGame extends JFrame {
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;
    private static final int FIXATION_TIME = 400; // ms
    private static final int STIMULUS_TIME = 300; // ms
    private static final int DELAY_TIME = 3000; // ms
    private static final int RESPONSE_WINDOW = 1800; // ms
    private static final int TOTAL_RESPONSE_TIME = 2500; // ms

    private JLabel displayLabel;
    private JTextField inputField;
    private Timer timer;
    private char firstStimulus;
    private char secondStimulus;
    private long responseStartTime;
    private boolean isResponded;
    private int score;

    public AXGame() {
        setTitle("AX Version Continuous Operation Test");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        displayLabel = new JLabel("", SwingConstants.CENTER);
        panel.add(displayLabel, BorderLayout.CENTER);

        inputField = new JTextField();
        inputField.setHorizontalAlignment(JTextField.CENTER);
        inputField.setEditable(false);
        panel.add(inputField, BorderLayout.SOUTH);

        add(panel);

        setFocusable(true);
        requestFocusInWindow();
        inputField.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if (!isResponded && (evt.getKeyChar() == '1' || evt.getKeyChar() == '2')) {
                    isResponded = true;
                    long reactionTime = System.currentTimeMillis() - responseStartTime;
                    checkResponse(evt.getKeyChar());
                    updateDisplay("");
                    startDelayTimer();
                }
            }
        });

        startFixationTimer();
    }

    private void startFixationTimer() {
        timer = new Timer(FIXATION_TIME, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                showFirstStimulus();
            }
        });
        timer.setRepeats(false);
        timer.start();
    }

    private void showFirstStimulus() {
        Random random = new Random();
        firstStimulus = random.nextInt(10) < 8 ? 'A' : 'B';
        updateDisplay(String.valueOf(firstStimulus));
        startStimulusTimer();
    }

    private void startStimulusTimer() {
        timer = new Timer(STIMULUS_TIME, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                updateDisplay("");
                startSecondStimulusTimer();
            }
        });
        timer.setRepeats(false);
        timer.start();
    }

    private void startSecondStimulusTimer() {
        timer = new Timer(DELAY_TIME + STIMULUS_TIME, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                showSecondStimulus();
            }
        });
        timer.setRepeats(false);
        timer.start();
    }

    private void showSecondStimulus() {
        Random random = new Random();
        secondStimulus = random.nextInt(10) < 8 ? 'X' : 'Y';
        updateDisplay(String.valueOf(secondStimulus));
        responseStartTime = System.currentTimeMillis();
        isResponded = false;
        startResponseTimer();
    }

    private void startResponseTimer() {
        timer = new Timer(RESPONSE_WINDOW, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                if (!isResponded) {
                    updateDisplay("Too slow! Score: " + score);
                    startDelayTimer();
                }
            }
        });
        timer.setRepeats(false);
        timer.start();
    }
    private void checkResponse(char key) {
        String correctKey = (firstStimulus == 'A' && secondStimulus == 'X') ? "1" : "2";
        if (String.valueOf(key).equals(correctKey)) {
            score++;
            updateDisplay("Correct! Score: " + score);
        } else {
            updateDisplay("Incorrect! Score: " + score);
        }
    }

    private void startDelayTimer() {
        timer = new Timer((int) (TOTAL_RESPONSE_TIME - (System.currentTimeMillis() - responseStartTime)), new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                startFixationTimer();
            }
        });
        timer.setRepeats(false);
        timer.start();
    }

    private void updateDisplay(String text) {
        displayLabel.setText(text);
        inputField.setText("");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            AXGame game = new AXGame();
            game.setVisible(true);
        });
    }
}






